package com.fr.base.core.list;

import com.fr.base.FRContext;
import com.fr.base.core.util.SerializationUtils;
import com.fr.base.file.CacheManager;
import com.fr.util.Utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ReadOnlyCacheList extends AbstractList
{
  private File diskDirectory = null;
  private int pool_size = 800;
  private String prefix = "ROCache";
  private List diskFileList = new ArrayList();
  private int total_size = 0;
  private int total_pool_size = 0;
  private Object[] lastPool;
  private int lastSize = 0;
  private int cachedPoolIndex = -1;
  private Object[] cachedPool;

  public ReadOnlyCacheList()
  {
    this.lastPool = new Object[this.pool_size];
  }

  public ReadOnlyCacheList(int paramInt, String paramString)
  {
    this.pool_size = paramInt;
    this.prefix = paramString;
    this.lastPool = new Object[this.pool_size];
  }

  public boolean add(Object paramObject)
  {
    if (this.lastSize < this.pool_size)
    {
      this.lastPool[(this.lastSize++)] = paramObject;
      this.total_size += 1;
    }
    if (this.lastSize >= this.pool_size)
      try
      {
        File localFile = new File(getCacheDirectory(), getPrefix() + "_" + System.currentTimeMillis() + "_" + (int)(Math.random() * 1000.0D));
        for (int i = 0; localFile.exists(); ++i)
          localFile = new File(getCacheDirectory(), getPrefix() + "_" + System.currentTimeMillis() + "_" + (int)(Math.random() * 1000.0D) + i);
        localFile.deleteOnExit();
        SerializationUtils.serializable(this.lastPool, localFile);
        this.lastSize = 0;
        this.diskFileList.add(localFile);
        this.total_pool_size += this.pool_size;
      }
      catch (Exception localException)
      {
        FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
      }
    return true;
  }

  public Object get(int paramInt)
  {
    if (paramInt >= this.pool_size * this.diskFileList.size())
      return this.lastPool[(paramInt - this.pool_size * this.diskFileList.size())];
    if ((this.cachedPoolIndex >= 0) && (this.cachedPoolIndex < this.diskFileList.size()) && (paramInt >= this.cachedPoolIndex * this.pool_size) && (paramInt < (this.cachedPoolIndex + 1) * this.pool_size))
      return this.cachedPool[(paramInt - this.cachedPoolIndex * this.pool_size)];
    flush_cached_pool(paramInt);
    return this.cachedPool[(paramInt - this.cachedPoolIndex * this.pool_size)];
  }

  private void flush_cached_pool(int paramInt)
  {
    this.cachedPoolIndex = (paramInt / this.pool_size);
    try
    {
      File localFile = (File)this.diskFileList.get(this.cachedPoolIndex);
      ObjectInputStream localObjectInputStream = new ObjectInputStream(new FileInputStream(localFile));
      this.cachedPool = ((Object[])localObjectInputStream.readObject());
      localObjectInputStream.close();
    }
    catch (IOException localIOException)
    {
      FRContext.getLogger().log(Level.WARNING, localIOException.getMessage(), localIOException);
    }
    catch (ClassNotFoundException localClassNotFoundException)
    {
      FRContext.getLogger().log(Level.WARNING, localClassNotFoundException.getMessage(), localClassNotFoundException);
    }
  }

  public int size()
  {
    return this.total_size;
  }

  public void clear()
  {
    this.cachedPoolIndex = -1;
    this.cachedPool = null;
    this.lastPool = new Object[this.pool_size];
    this.lastSize = 0;
    this.total_pool_size = 0;
    this.total_size = 0;
    this.diskFileList.clear();
    if (this.diskDirectory != null)
      Utils.deleteFile(this.diskDirectory);
  }

  public String getPrefix()
  {
    return this.prefix;
  }

  public void setPrefix(String paramString)
  {
    this.prefix = paramString;
  }

  private File getCacheDirectory()
  {
    if (this.diskDirectory == null)
    {
      this.diskDirectory = new File(FRContext.getCacheManager().getCacheDirectory(), "DATA_" + System.currentTimeMillis() + "_" + (int)(Math.random() * 1000.0D));
      for (int i = 0; this.diskDirectory.exists(); ++i)
        this.diskDirectory = new File(FRContext.getCacheManager().getCacheDirectory(), "DATA_" + System.currentTimeMillis() + "_" + (int)(Math.random() * 1000.0D) + i);
      Utils.mkdirs(this.diskDirectory);
      this.diskDirectory.deleteOnExit();
    }
    return this.diskDirectory;
  }

  protected void finalize()
    throws Throwable
  {
    finalize();
    if (this.diskDirectory != null)
      Utils.deleteFile(this.diskDirectory);
  }
}